﻿@model ForumTopicPageModel
@{
    Layout = "_ColumnsOne";

    //title
    Html.AddTitleParts(Model.Subject);
    //page class
    Html.AppendPageCssClassParts("html-forum-topic-page");
}
<div class="page forum-topic-page">
    @await Component.InvokeAsync("ForumBreadcrumb", new { forumTopicId = Model.Id })
    @await Html.PartialAsync("_ForumHeader")
    @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.BoardsTopicAfterHeader, additionalData = Model })
    <nop-antiforgery-token />
    <div class="topic-name">
        <h1>@Model.Subject</h1>
    </div>
    <div class="topic-actions upper">
        <div class="actions">
            @if (Model.IsCustomerAllowedToEditTopic)
            {
                @Html.RouteLink(T("Forum.EditTopic").Text, "TopicEdit", new { id = Model.Id }, new { @class = "edit-topic-button" })
            }
            @if (Model.IsCustomerAllowedToDeleteTopic)
            {
                <a href="#" class="delete-topic-button" onclick="return deletetopic(@(Model.Id))">@T("Forum.DeleteTopic").Text</a>
                <script asp-location="Footer">
                    function deletetopic(topicId) {
                        if (confirm('@T("Common.AreYouSure")')) {
                            var postData = {
                                id: topicId
                            };
                            addAntiForgeryToken(postData);
                            $.ajax({
                                cache: false,
                                type: "POST",
                                url: "@Url.Action("TopicDelete", "Boards")",
                                data: postData,
                                dataType: "json",
                                success: function (data, textStatus, jqXHR) {
                                    location.href = data.redirect;
                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                    alert('Failed to delete');
                                }
                            });
                        }
                        return false;
                    }
                </script>
            }
            @if (Model.IsCustomerAllowedToMoveTopic)
            {
                @Html.RouteLink(T("Forum.MoveTopic").Text, "TopicMove", new { id = Model.Id }, new { @class = "move-topic-button" })
            }
            @Html.RouteLink(T("Forum.Reply").Text, "PostCreate", new { id = Model.Id }, new { @class = "reply-topic-button" })
            @if (Model.IsCustomerAllowedToSubscribe)
            {
                <a class="watch-topic-button" href="#" id="watch-topic-top">@Model.WatchTopicText</a>
                <script asp-location="Footer">
                    $(document).ready(function () {
                        $('#watch-topic-top').on('click', function () {
                            $.ajax({
                                cache: false,
                                type: "POST",
                                url: "@Url.RouteUrl("TopicWatch", new { id = Model.Id })",
                                dataType: "json",
                                success: function (data, textStatus, jqXHR) {
                                    $('#watch-topic-top').text(data.Text);
                                    $('#watch-topic-bottom').text(data.Text);
                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                    alert('Failed to watch');
                                }
                            });
                        });
                    });
                </script>
            }
        </div>
        @{
            var pager = Html.Pager(new PagerModel
            {
                PageSize = Model.PostsPageSize,
                TotalRecords = Model.PostsTotalRecords,
                PageIndex = Model.PostsPageIndex,
                ShowTotalSummary = false,
                RouteActionName = "TopicSlugPaged",
                UseRouteLinks = true,
                RouteValues = new RouteValues { id = Model.Id, slug = Model.SeName }
            });
        }
        @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.BoardsTopicTop, additionalData = Model })
        @if (!string.IsNullOrEmpty(pager.ToHtmlString()))
        {
            <div class="pager upper">
                @pager
            </div>
        }
    </div>

    <div class="topic-posts">
        @foreach (var post in @Model.ForumPostModels)
        {
            await Html.RenderPartialAsync("_ForumPost", post);
        }
    </div>

    <div class="topic-actions lower">
        <div class="actions">
            @Html.RouteLink(T("Forum.Reply").Text, "PostCreate", new {id = Model.Id}, new {@class = "reply-topic-button"})
            @if (Model.IsCustomerAllowedToSubscribe)
            {
                <a class="watch-topic-button" href="#" id="watch-topic-bottom">@Model.WatchTopicText</a>
                <script asp-location="Footer">
                    $(document).ready(function() {
                        $('#watch-topic-bottom').on('click', function() {
                            var postData = {};
                            addAntiForgeryToken(postData);

                            $.ajax({
                                cache: false,
                                type: "POST",
                                url: "@Url.RouteUrl("TopicWatch", new {id = Model.Id})",
                                data: postData,
                                dataType: "json",
                                success: function (data, textStatus, jqXHR) {
                                    $('#watch-topic-top').text(data.Text);
                                    $('#watch-topic-bottom').text(data.Text);
                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                    alert('Failed to watch');
                                }
                            });
                        });
                    });
                </script>
            }
        </div>
        @if (!string.IsNullOrEmpty(pager.ToHtmlString()))
        {
            <div class="pager lower">
                @pager
            </div>
        }
    </div>
    @await Component.InvokeAsync("Widget", new { widgetZone = PublicWidgetZones.BoardsTopicBottom, additionalData = Model })
</div>
<script asp-location="Footer">
    function deletepost(postId) {
        if (confirm('@T("Common.AreYouSure")')) {
            var postData = {
                id: postId
            };
            addAntiForgeryToken(postData);
            $.ajax({
                cache: false,
                type: "POST",
                url: "@Url.Action("PostDelete", "Boards")",
                data: postData,
                dataType: "json",
                success: function (data, textStatus, jqXHR) {
                    location.href = data.redirect;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert('Failed to delete');
                }
            });
        }
        return false;
    }
</script>